package com.sc.nft.dao;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.DaoIdentity;
import com.sc.nft.entity.vo.AdminSimpleDaoIdentityVO;
import com.sc.nft.entity.vo.CatenaIdsGroupByCatenaVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.mapper.DaoIdentityMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * DAO身份表(sc_dao_identity)数据DAO
 *
 * @author zxy
 * @description
 * @since 2023-10-20 00:56:41
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class DaoIdentityDao extends ServiceImpl<DaoIdentityMapper, DaoIdentity> {
    private final UserCollectionDao userCollectionDao;
    private final RedisTemplate redisTemplate;

    /**
     * 清除DAO信息
     *
     * @param daoId
     */
    public void clearByDaoId(Long daoId) {
        LambdaUpdateWrapper<DaoIdentity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DaoIdentity::getDaoId, daoId);
        remove(wrapper);
    }

    public BigDecimal getMaxRatio(Long daoId, Long userId) {
        return baseMapper.getMaxRatio(daoId, userId);
    }

    public List<DaoIdentity> getByDaoId(Long daoId) {
        return lambdaQuery().eq(DaoIdentity::getDaoId, daoId).orderByAsc(DaoIdentity::getLevel).list();
    }

    public DaoIdentity getMaxIdentity(Long daoId, Long userId) {
        List<DaoIdentity> daoIdentities = getByDaoId(daoId);
        for (int i = daoIdentities.size() - 1; i > 0; i--) {
            boolean has = userCollectionDao.userHasCollection(userId, daoIdentities.get(0).getCollectionId());
            if (has) {
                return daoIdentities.get(0);
            }
        }
        return null;
    }

    public List<CatenaIdsGroupByCatenaVO> selectByCatenaIdsGroupByCatena(List<Long> ids) {
        // 翔宇要求,粉丝数可以不准,不能影响服务器级别.不准就不准了,当下无所谓 时间:2024-05-11 18:29:44
        String key = RedisKeyEnum.CATENA_IDS_GROUP_BY_CATENA.getKey();
        if (redisTemplate.hasKey(key)) {
            return JSONObject.parseArray(redisTemplate.opsForValue().get(key).toString(), CatenaIdsGroupByCatenaVO.class);
        }
        List<CatenaIdsGroupByCatenaVO> catenaIdsGroupByCatenaVOS = baseMapper.selectByCatenaIdsGroupByCatena(ids);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(catenaIdsGroupByCatenaVOS), 1, TimeUnit.DAYS);
        return catenaIdsGroupByCatenaVOS;
    }


    public DaoIdentity getMaxLevelDetails(Long daoId) {
        LambdaQueryWrapper<DaoIdentity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DaoIdentity::getDaoId, daoId);
        wrapper.orderByDesc(DaoIdentity::getLevel);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public Page<AdminSimpleDaoIdentityVO> getSimpleDaoIdentityPage(Long daoId, String collectionName, String identityName, Integer pageNo, Integer pageSize) {
        return baseMapper.getSimpleDaoIdentityPage(new Page<>(pageNo, pageSize), daoId, collectionName, identityName);
    }

    public Page<DaoIdentity> pageByDaoIdAndFirstTitle(Page page, Long daoId, String firstTitle) {
        return baseMapper.pageByDaoIdAndFirstTitle(page, daoId, firstTitle);
    }

}